package com.ruoyi.web.controller.resourceInfo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.resource.domain.ResourceInfo;
import com.ruoyi.resource.domain.ResourceInfoVO;
import com.ruoyi.resource.service.IFileOperator;
import com.ruoyi.resource.service.IResourceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 素材信息Controller
 * 像删除，替换接口走这里
 * 
 * @author ruoyi
 * @date 2023-05-14
 */
@RestController
@RequestMapping("/resource/info")
public class ResourceInfoController extends BaseController
{
    @Autowired
    private IResourceInfoService resourceInfoService;

    @Autowired
    IFileOperator iFileOperator;

    /**
     * 查询资源信息列表
     */
//    @PreAuthorize("@ss.hasPermi('resource:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResourceInfo resourceInfo)
    {
        startPage();
        List<ResourceInfoVO> list = resourceInfoService.selectResourceInfoList(resourceInfo);
        return getDataTable(list);
    }

//    /**
//     * 导出资源信息列表
//     */
//    @PreAuthorize("@ss.hasPermi('resource:info:export')")
//    @Log(title = "资源信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ResourceInfo resourceInfo)
//    {
//        List<ResourceInfoVO> list = resourceInfoService.selectResourceInfoList(resourceInfo);
//        ExcelUtil<ResourceInfo> util = new ExcelUtil<ResourceInfo>(ResourceInfo.class);
//        util.exportExcel(response, list, "资源信息数据");
//    }

    /**
     * 获取资源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(resourceInfoService.selectResourceInfoById(id));
    }

    /**
     * 新增资源信息
     */
    @PreAuthorize("@ss.hasPermi('resource:info:add')")
    @Log(title = "资源信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResourceInfo resourceInfo)
    {
        return toAjax(resourceInfoService.insertResourceInfo(resourceInfo));
    }

    /**
     * 修改资源信息
     */
    @PreAuthorize("@ss.hasPermi('resource:info:edit')")
    @Log(title = "资源信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResourceInfo resourceInfo)
    {
        return toAjax(resourceInfoService.updateResourceInfo(resourceInfo));
    }

    /**
     * 删除资源信息
     * step1: 修改数据库中的记录
     */
    @PreAuthorize("@ss.hasPermi('resource:info:remove')")
    @Log(title = "资源信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //轮训删除信息
        for(Long id: ids) {
            ResourceInfo resourceInfo = resourceInfoService.selectResourceInfoById(id);
            // 将该行记录设置为没有
            if (resourceInfo != null) {
                resourceInfo.setHasResource(0L);
                resourceInfoService.updateResourceInfo(resourceInfo);
            }
            //删文件
             iFileOperator.DeleteFileByScreenId(Math.toIntExact(id));
        }
        return toAjax(ids.length);
    }
}
